package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.MenuMapper;
import com.bwie.pojo.Menu;
import com.bwie.utils.MD5Utils;
import com.bwie.utils.R;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.val;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.mapper.UserMapper;
import com.bwie.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.swing.tree.TreeNode;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public R userLogin(User user) {

        //密码验证
        Pattern PwdCompile = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,}$");
        Matcher matcherPwd = PwdCompile.matcher(user.getUserPwd());
        boolean matchesPwd = matcherPwd.matches();
        String encrypt = "";
        if (matchesPwd) {
            encrypt = MD5Utils.encrypt(user.getUserPwd());
            System.out.println("密码加密:" + encrypt);
        }else {
            return R.ERROR(500,"密码格式错误");
        }
//        User userOne = userMapper.selectOne(
//                new QueryWrapper<User>().lambda().eq(User::getUserAccount,user.getUserAccount())
//                        .or().eq(User::getUserEmail,user.getUserAccount())
//                        .or().eq(User::getUserPhone,user.getUserAccount())
//        );
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(user.getUserAccount())){
            wrapper.lambda().or().eq(User::getUserAccount,user.getUserAccount());
        }
        if (ObjectUtils.isNotEmpty(user.getUserPhone())){
            Pattern PhoneCompile = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
            Matcher matcherPhone = PhoneCompile.matcher(user.getUserPhone());
            boolean matchesPhone = matcherPhone.matches();
            if (matchesPhone){
                wrapper.lambda().or().eq(User::getUserPhone,user.getUserPhone());
            }else {
                return R.ERROR(500,"手机号格式错误");
            }
        }
        if (ObjectUtils.isNotEmpty(user.getUserEmail())){
            wrapper.lambda().or().eq(User::getUserEmail,user.getUserEmail());
        }

        User userOne = userMapper.selectOne(wrapper);

        if (ObjectUtils.isEmpty(userOne)){
            return R.ERROR(500,"账户错误，请输入正确账户");
        }
        if (!userOne.getUserPwd().equals(encrypt)){
            return R.ERROR(500,"账户或密码错误");
        }
        String token = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,"bwie")
                .claim("userId",userOne.getUserId()).compact();

        redisTemplate.opsForValue().set("token:"+userOne.getUserId(),token,15, TimeUnit.MINUTES);

        userOne.setToken(token);

        return R.OK(userOne);
    }


}
